﻿using HuiZe.education.Model;
using HuiZe.education.Resource.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HuiZe.education.View
{
    /// <summary>
    /// Win_Lesson_MyPackage_Viewer.xaml 的交互逻辑
    /// </summary>
    public partial class Win_Lesson_MyShare_Viewer : HuiZeWindow
    {
        public Win_Lesson_MyShare_Viewer()
        {
            InitializeComponent();
            PART_Back.Click += PART_Back_Click;
            PART_Prev.Click += PART_Prev_Click;
            PART_Next.Click += PART_Next_Click;

            this.WindowStyle = WindowStyle.None;
            this.Background = Brushes.Transparent;
            this.ResizeMode = ResizeMode.NoResize;
            this.WindowState = WindowState.Maximized;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
        }

        void PART_Next_Click(object sender, RoutedEventArgs e)
        {
            _index++;
            _list_rb[_index].IsChecked = new bool?(true);
            Refresh();
        }

        void PART_Prev_Click(object sender, RoutedEventArgs e)
        {
            _index--;
            _list_rb[_index].IsChecked = new bool?(true);
            Refresh();
        }

        void PART_Back_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = new bool?(false);
        }

        public void Load(List<ResBkBeginClassResListModel> _list_model, int _id = -1)
        {
            if (_list_model == null || _list_model.Count == 0) return;

            List<resCourseListChildModel> _list = new List<resCourseListChildModel>();
            foreach (var _item in _list_model)
            {
                _list.Add(new resCourseListChildModel
                {
                    id = _item.id,
                    resources_name = _item.bname,
                    file = _item.file,
                    ext_name = _item.extname,
                    cover = _item.image_cover,
                    attachment_id = _item.attachment_id,
                    resources_tag_id = Convert.ToInt32(_item.resources_id),
                    lyric = _item.lyric,
                    rotation_img = _item.rotation_img,
                    rotation_img_text = _item.rotation_img_text,
                    lyric_text = _item.lyric_text,
                    admin_id = _item.user_id,
                });
            }

            Load(_list, _id);
        }
        public void Load(List<resCourseListChildModel> _list_model, int _id = -1)
        {
            Reset();
            _is_selected_default = false;
            _list_id = new List<int>();
            _list_rb = new List<RadioButton>();
            if (_list_model == null || _list_model.Count == 0) return;

            _id_selected = _id;
            PART_LessonMyPackage_Items.ItemsSource = _list_model;
            _list_id = _list_model.Select(x => x.id).ToList();
            _index = _list_id.IndexOf(_id);
            Refresh();
        }

        void PART_LessonMyPackage_Item_Loaded(object sender, RoutedEventArgs e)
        {
            RadioButton _rb = sender as RadioButton;
            _rb.Checked += PART_CoursesResourceItem_Checked;
            _list_rb.Add(_rb);

            if (_id_selected >= 0)
            {
                var _model = _rb.DataContext as resCourseListChildModel;
                if (_model.id == _id_selected)
                {
                    _is_selected_default = true;
                    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, new Action(() =>
                    {
                        _rb.IsChecked = new bool?(true);
                    }));
                    return;
                }
            }

            if (!_is_selected_default)
            {
                _is_selected_default = true;
                _rb.IsChecked = new bool?(true);
            }
        }

        void PART_CoursesResourceItem_Checked(object sender, RoutedEventArgs e)
        {
            resCourseListChildModel _item = ((RadioButton)sender).DataContext as resCourseListChildModel;
            PART_Media.IsAutoPlay = true;
            PART_Media.SetData(_item);
        }

        void Refresh()
        {
            PART_Current.Text = $"{_index + 1}/{_list_id.Count}";
            PART_Prev.IsEnabled = _index > 0;
            PART_Next.IsEnabled = _index < (_list_id.Count - 1);
        }

        public void Reset()
        {
            PART_Media.Reset();
            PART_LessonMyPackage_Items.ItemsSource = null;
        }


        int _index;
        int _id_selected;
        List<int> _list_id;
        List<RadioButton> _list_rb;
        bool _is_selected_default;
    }
}
